#!/usr/bin/env python

import model_data
from topics.swingset_request_handler import SwingsetRequestHandler

class XSS(SwingsetRequestHandler):
    def index(self):
        # This is a 301 temporary redirect
        self.redirect( self.url(c="xss", action="no_escaping") )
        
    def no_escaping(self):
        rules = model_data.get_all_rules();
        for rule in rules:
            for context in rule.contexts:
                context.input = self.request.get("input" + context.context_code)
                context.encoded_input = context.input
        
        self.render_and_write("xss/no_escaping.html", 
                xss_rules=rules, 
                )
            
    def esapi(self):
        encoder = 'esapi'
        rules = model_data.get_rules_with_encoder(encoder);
        for rule in rules:
            for context in rule.contexts:
                context.input = self.request.get("input" + context.context_code)
                context.encoded_input = rule.encoders[encoder].encode(context.input)
        
        
        self.render_and_write("xss/esapi.html", 
                xss_rules=rules, 
                )
                
    def django(self):
        encoder = 'django'
        rules = model_data.get_rules_with_encoder(encoder);
        for rule in rules:
            for context in rule.contexts:
                context.input = self.request.get("input" + context.context_code)
                context.encoded_input = rule.encoders[encoder].encode(context.input)
        
        
        self.render_and_write("xss/django.html", 
                xss_rules=rules, 
                )

